﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.PhongHocService;
namespace eTMS.Controllers{
public class PhongHocControllerBase : CommonController{
	public PhongHocControllerBase()
		: base()
	{	
		this.dao = new PhongHocServiceClient();
	}
	~PhongHocControllerBase(){
		dao.Close();
	}
	private DataTable _Table;
	private PhongHocEntity oPhongHoc;	
	public PhongHocEntity PhongHocObject{
		get{
			return oPhongHoc;
		}
		set{
			oPhongHoc=value;
		}
	}
	protected PhongHocServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSPhongHoc(false);			
			return _Table;
		}
	}
	public void LoadAllDSPhongHoc(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oPhongHoc==null)return false;
		bool result=true;
		try{
			oPhongHoc = dao.SaveRet(oPhongHoc);
		}
		catch{
			result=false;
		}
		if(result){
			_ListPhongHoc.Add(oPhongHoc);
		}
		return result;
	}
	public bool Update(){
		if(oPhongHoc==null)return false;
		return dao.Update(oPhongHoc);
	}
	public bool Delete(){
		if(oPhongHoc==null)return false;
		bool retvalue = dao.Delete(oPhongHoc);
		if(retvalue){			
			_ListPhongHoc.Remove(oPhongHoc);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 PhongHoc_ID){
		bool retvalue = dao.DeleteByID(PhongHoc_ID);		
		if(retvalue){
			PhongHocEntity oDeleted=FindPhongHoc(PhongHoc_ID);
			if(oDeleted!=null){
				_ListPhongHoc.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oPhongHoc = new PhongHocEntity();
	}
	//Check if exists MaPhong
	public bool CheckExistsMaPhong(object value){	
		return dao.SelectOneByMaPhong(value.ToString()).MaPhong!=null;
    }    
    //Copy data from data row in grid to object
    public PhongHocEntity MapDataRowToObject(DataRow row){
		PhongHocEntity o = new PhongHocEntity();
		 o.MaPhong=Convert.ToString(row["MaPhong"]);
		 if(!row.IsNull("SucChua"))
			o.SucChua=ConvertInt(row["SucChua"]);
		 if(!row.IsNull("Tang"))
			o.Tang=ConvertInt(row["Tang"]);
		 if(!row.IsNull("ThuTu"))
			o.ThuTu=ConvertInt(row["ThuTu"]);
		 if(!row.IsNull("KhuNha_ID"))
			o.KhuNha_ID=(Int32)(row["KhuNha_ID"]);
		 if(!row.IsNull("PhongHoc_ID"))
		    o.PhongHoc_ID=(Int32)(row["PhongHoc_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(PhongHocEntity o,DataRow row){
		row["MaPhong"]=o.MaPhong;
		row["SucChua"]=o.SucChua;
		row["Tang"]=o.Tang;
		row["ThuTu"]=o.ThuTu;
		row["KhuNha_ID"]=o.KhuNha_ID;
		row["PhongHoc_ID"]=o.PhongHoc_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.PhongHocObject,row);
    }
	public void LoadObject(Int32 PhongHoc_ID){
		PhongHocEntity oLoad=FindPhongHoc(PhongHoc_ID);
		if(oLoad==null){
			oPhongHoc=this.LoadPhongHoc(PhongHoc_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oPhongHoc.IsNullPhongHoc_ID;
			if(!bNotIsNull){
				this.ListPhongHoc.Add(oPhongHoc);
			}
		}
		else{
			oPhongHoc=oLoad;
		}
	}
	#region CachedList PhongHoc
	private static List<PhongHocEntity> _ListPhongHoc=new List<PhongHocEntity>();
    public List<PhongHocEntity>ListPhongHoc{
		get{
			return _ListPhongHoc;
		}
	}
	public static List<PhongHocEntity>ListPhongHocStatic{
		get{
			return _ListPhongHoc;
		}
	}
	public PhongHocEntity FindPhongHoc(Int32 PhongHoc_ID){
		PhongHocEntity oResult=this.ListPhongHoc.Find(delegate(PhongHocEntity oFind){
			bool result=true; 
			result&=oFind.PhongHoc_ID.Equals(PhongHoc_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllPhongHoc(bool clearCache){
		if(_ListPhongHoc.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		PhongHocServiceClient dao = new PhongHocServiceClient();
		_ListPhongHoc=new List<PhongHocEntity>(dao.SelectAll());
		dao.Close();
	}
	public PhongHocEntity LoadPhongHoc(Int32 PhongHoc_ID){			
		PhongHocEntity o=dao.SelectOne(PhongHoc_ID);		
		return o;
	}
	#endregion
}
}
